package com.game4U.Component.FlvPlayerComponent
{
	import flash.display.Sprite;
	import flash.events.Event;
	
	/**
	 * ...
	 * @author Feng
	 */
	public class FlvPlayback extends Sprite 
	{
		private var _flvPlayer:FlvPlayer;
		private var _flvSkin:FlvSkin;
		
		public function FlvPlayback(w:Number = 640, h:Number = 480, autoPlay:Boolean = true, url:String = "" ) 
		{
			this.addEventListener(Event.REMOVED_FROM_STAGE, clear);
			
			_flvPlayer = new FlvPlayer(w, h, autoPlay, url);
			_flvPlayer.addEventListener("FLV_PLAY_COMPLETE_EVENT", OnFlvPlayComplete);
			addChild(_flvPlayer);
			
			_flvSkin = new FlvSkin();
			_flvSkin.addEventListener("FLV_PAUSE_EVENT", OnPause);
			_flvSkin.addEventListener("FLV_PLAY_EVENT", OnPlay);
			if (autoPlay) {
				_flvSkin.Pause();
			}else {
				_flvSkin.PlayFLV();
			}
			
			_flvSkin.x = (w - _flvSkin.width) / 2;
			_flvSkin.y = h - _flvSkin.height - 10;
			addChild(_flvSkin);
			
			addEventListener(Event.ENTER_FRAME,enterHandler);
		}
		
		private function clear(evt:Event) {
			this.removeEventListener(Event.REMOVED_FROM_STAGE, clear);
			_flvPlayer.removeEventListener("FLV_PLAY_COMPLETE_EVENT", OnFlvPlayComplete);
			_flvSkin.removeEventListener("FLV_PAUSE_EVENT", OnPause);
			_flvSkin.removeEventListener("FLV_PLAY_EVENT", OnPlay);
			removeEventListener(Event.ENTER_FRAME,enterHandler);
		}
		
		private function enterHandler(evt:Event) {
			if (_flvPlayer.IsPlay) {
				_flvSkin.SetProgress(_flvPlayer.playPercent);
			}
			
		}
		
		private function OnPlay(evt:Event) {
			_flvPlayer.Play();
		}
		
		private function OnPause(evt:Event) {
			_flvPlayer.Pause();
		}
		
		private function OnFlvPlayComplete(evt:Event) {
			_flvSkin.PlayFLV();
		}
		
		
	}

}